<template>
  <view class="container">
    <!-- 左侧二级导航栏（从上到下） -->
    <view class="left-sidebar" :class="{ 'collapsed': isSidebarCollapsed }">
      <view class="zhuzi" v-if="!isSidebarCollapsed">
        供应链管理平台
      </view>
      <view 
        v-for="(item, index) in currentSubMenuList" 
        :key="'sub-' + index"
        class="sub-menu-item"
        :class="{ 'sub-active': currentSubIndex === index && currentTopIndex === activeTopIndex }"
        @click="handleSubMenuClick(index, item)"
      >
        <text :class="'iconfont ' + item.icon"></text>
        <text v-if="!isSidebarCollapsed">{{ item.title }}</text>
      </view>
    </view>

    <!-- 右侧主区域 -->
    <view class="right-content" :class="{ 'expanded': isSidebarCollapsed }">
      <!-- 右上方一级导航栏（横向） -->
      <view class="top-nav">
        <!-- 将展开/折叠按钮放在这里 -->
        <view class="sidebar-toggle" @click="toggleSidebar">
          <text class="iconfont">{{ isSidebarCollapsed ? '展开' : '折叠' }}</text>
        </view>
        
        <view 
          v-for="(item, index) in topMenuList" 
          :key="'top-' + index"
          class="nav-item"
          :class="{ 'nav-active': currentTopIndex === index && !isHomePage }"
          @click="handleTopMenuClick(index)"
        >
          {{ item.title }}
        </view>
      </view>
      
      <!-- 面包屑导航区域 -->
      <view class="breadcrumb-container" v-if="breadcrumbs.length > 0 || isHomePage">
        <view class="breadcrumb-item home-crumb" :class="{ 'active': isHomePage }" @click="switchToHomePage">
          <text>首页</text>
        </view>
        <view 
          v-for="(breadcrumb, index) in breadcrumbs" 
          :key="'crumb-'+index"
          class="breadcrumb-item"
          :class="{ 'active': isBreadcrumbActive(breadcrumb) }"
          @click="switchToBreadcrumb(breadcrumb)"
        >
          <text>{{ breadcrumb.subTitle }}</text>
          <text class="close-breadcrumb" @click.stop="removeBreadcrumb(index)">×</text>
        </view>
      </view>
      
      <!-- 右侧内容区域 -->
      <view class="main-content">
        <view class="export-btn" v-if="showExport">
        </view>
        
        <!-- 使用 keep-alive 缓存所有组件 -->
        <keep-alive>
          <component 
            :is="currentComponent" 
            :key="currentComponentKey"
            ref="activeComponent"
          ></component>
        </keep-alive>
        
        <!-- 分页控件 -->
        <view class="pagination" v-if="showPagination">
          <text>共557条</text>
          <text>50条/页</text>
          <view class="page-numbers">
            <text v-for="page in 12" :key="page" :class="{ 'current-page': page === 1 }">{{ page }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import index from '../index/index.vue'
import xsmxVue from '../index/xsmx/xsmx.vue';
import zhuceVue from '../zhu/zhuce.vue';
import xspmVue from '../index/xsmx/xspm.vue';
import xshzVue from '../index/xsmx/xshz.vue';
import jhtjVue from '../index/xsmx/jhtj.vue';
import thtjVue from '../index/xsmx/thtj.vue';
import jczlVue from '../jczl/jczl.vue';
import ywdjVue from '../ywdj/ywdj.vue';
import ysdVue from '../ywdj/ysd.vue';
import roleVue from '../index/system/role/role.vue';
import userVue from '../index/system/user/user.vue';

import thdVue from '../ywdj/thd.vue';
import homeVue from '../shouye/shouye.vue'; // 首页组件
import jjtzVue from '../ywdj/jjtz.vue';
import jszdVue from '../jszd/jszd.vue';
import lyzdVue from '../jszd/lyzd.vue';
import { markRaw } from 'vue';

export default {
  data() {
    return {
      isSidebarCollapsed: false,
      currentTopIndex: 0,       // 当前选中的一级菜单索引
      activeTopIndex: -1,       // 当前活动内容对应的一级菜单索引，-1表示首页
      currentSubIndex: 0,       // 当前选中的二级菜单索引
      activeSubIndex: -1,       // 当前活动内容对应的二级菜单索引，-1表示首页
      breadcrumbs: [],
      currentComponentKey: '',  // 用于标识当前组件
      componentStates: {},      // 存储各个组件的状态
      topMenuList: [
        { title: "系统管理", category: "xitong" },
        { title: "供应商", category: "sales" },
        { title: "基础资料", category: "purchase" },
        { title: "业务单据", category: "ywdj" },
        { title: "数据分析", category: "sjfenxi" },
        { title: "结算账单", category: "jszd" },
        { title: "关联功能", category: "sales" },
      ],
      subMenuData: {
        xitong: [
          { title: "用户管理", icon: "icon-overview", component: "userVue" },
          { title: "角色管理", icon: "icon-products",component:"roleVue" }
        ],
        sales: [
          { title: "供应商信息", icon: "icon-ranking" },
          { title: "供应商等级", icon: "icon-summary" },
          { title: "供应商子账户", icon: "icon-detail" },
          { title: "子账户权限设置", icon: "icon-export" }
        ],
		jszd: [
		  { title: "经代销对账单", icon: "icon-ranking",component: "jszdVue" },
		  { title: "联营租贷对账单", icon: "icon-summary",component: "lyzdVue" },
		],
        purchase: [
          { title: "新品申请", icon: "icon-purchase" },
          { title: "商品信息", icon: "icon-return",component: "jczlVue"  },
          { title: "商品调价申请", icon: "icon-purchase"},
        ],
        ywdj: [
          { title: "采购订单", icon: "icon-ranking",component: "ywdjVue" },
          { title: "预约送货单申请", icon: "icon-summary" },
          { title: "进价调整单", icon: "icon-detail",component: "jjtzVue" },
          { title: "验收单", icon: "icon-export",component: "ysdVue" },
          { title: "退货单", icon: "icon-detail",component: "thdVue" },
          { title: "费用缴费单", icon: "icon-export" }
        ],
        sjfenxi: [
          { title: "库存查询", icon: "icon-purchase", component: "index" },
          { title: "进货统计", icon: "icon-return",component:"jhtjVue" },
          { title: "退货统计", icon: "icon-purchase", component:"thtjVue" },
          { title: "销售排名", icon: "icon-purchase", component: "xspmVue" },
          { title: "销售明细", icon: "icon-return", component: "xshzVue" },
          { title: "销售汇总", icon: "icon-purchase", component: "xsmxVue" },
        ]
      },
      componentsMap: markRaw({
        homeVue, // 新增的首页组件
        index,
        xsmxVue,
        zhuceVue,
        xspmVue,
        xshzVue,
        roleVue,
        userVue,
		jhtjVue,
		thtjVue,
		jczlVue,
		ywdjVue,
		ysdVue
        jhtjVue,
        thtjVue,
        jczlVue,
		ysdVue,
        ywdjVue,
        thdVue,
		jjtzVue,
		jszdVue,
		lyzdVue

      })
    };
  },
  computed: {
    isHomePage() {
      return this.activeTopIndex === -1 && this.activeSubIndex === -1;
    },
    currentSubMenuList() {
      const category = this.topMenuList[this.currentTopIndex].category;
      return this.subMenuData[category] || [];
    },
    activeSubMenuList() {
      if (this.activeTopIndex === -1) return [];
      const category = this.topMenuList[this.activeTopIndex].category;
      return this.subMenuData[category] || [];
    },
    currentComponent() {
      if (this.isHomePage) return this.componentsMap.homeVue;
      const componentName = this.activeSubMenuList[this.activeSubIndex]?.component;
      return componentName ? this.componentsMap[componentName] : null;
    },
    showPagination() {
      return this.activeTopIndex === 1 && this.activeSubIndex === 2;
    },
    showExport() {
      return this.activeTopIndex === 1 && this.activeSubIndex === 3;
    }
  },
  watch: {
    activeTopIndex() {
      this.updateComponentKey();
    },
    activeSubIndex() {
      this.updateComponentKey();
    }
  },
  methods: {
    toggleSidebar() {
      this.isSidebarCollapsed = !this.isSidebarCollapsed;
    },
    switchToHomePage() {
      if (!this.isHomePage) {
        // 保存当前组件状态
        this.saveComponentState();
        
        this.activeTopIndex = -1;
        this.activeSubIndex = -1;
        this.currentTopIndex = 0; // 重置一级菜单选中状态
        this.currentSubIndex = 0; // 重置二级菜单选中状态
      }
    },
    handleTopMenuClick(index) {
      if (this.currentTopIndex !== index) {
        this.currentTopIndex = index;
        this.currentSubIndex = 0; // 重置二级菜单选中状态
      }
    },
    handleSubMenuClick(index, item) {
      if (this.currentTopIndex !== this.activeTopIndex || index !== this.activeSubIndex) {
        // 保存当前组件状态
        this.saveComponentState();
        
        this.activeTopIndex = this.currentTopIndex;
        this.activeSubIndex = index;
        this.updateBreadcrumb();
      }
    },
    saveComponentState() {
      if (this.currentComponentKey && this.$refs.activeComponent) {
        // 获取组件实例并保存状态
        const componentInstance = this.$refs.activeComponent;
        if (componentInstance.getState) {
          this.componentStates[this.currentComponentKey] = componentInstance.getState();
        }
      }
    },
    restoreComponentState() {
      if (this.currentComponentKey && this.componentStates[this.currentComponentKey]) {
        // 恢复组件状态
        const componentInstance = this.$refs.activeComponent;
        if (componentInstance && componentInstance.setState) {
          componentInstance.setState(this.componentStates[this.currentComponentKey]);
        }
      }
    },
    updateComponentKey() {
      // 生成唯一的组件标识键
      this.currentComponentKey = this.isHomePage 
        ? 'home' 
        : `${this.activeTopIndex}-${this.activeSubIndex}`;
      
      // 在下次DOM更新后恢复状态
      this.$nextTick(() => {
        this.restoreComponentState();
      });
    },
    updateBreadcrumb() {
      if (this.isHomePage) return; // 首页不添加面包屑
      
      // 添加面包屑
      const newBreadcrumb = {
        topTitle: this.topMenuList[this.activeTopIndex].title,
        subTitle: this.activeSubMenuList[this.activeSubIndex].title,
        topIndex: this.activeTopIndex,
        subIndex: this.activeSubIndex
      };
      
      // 检查是否已存在相同的面包屑
      const existsIndex = this.breadcrumbs.findIndex(crumb => 
        crumb.topIndex === newBreadcrumb.topIndex && 
        crumb.subIndex === newBreadcrumb.subIndex
      );
      
      if (existsIndex === -1) {
        this.breadcrumbs.push(newBreadcrumb);
      }
    },
    removeBreadcrumb(index) {
      // 如果删除的是当前活动的面包屑，需要切换到首页
      if (this.isBreadcrumbActive(this.breadcrumbs[index])) {
        this.switchToHomePage();
      }
      this.breadcrumbs.splice(index, 1);
    },
    isBreadcrumbActive(breadcrumb) {
      return (
        this.activeTopIndex === breadcrumb.topIndex &&
        this.activeSubIndex === breadcrumb.subIndex
      );
    },
    switchToBreadcrumb(breadcrumb) {
      if (
        this.activeTopIndex !== breadcrumb.topIndex ||
        this.activeSubIndex !== breadcrumb.subIndex
      ) {
        // 保存当前组件状态
        this.saveComponentState();
        
        this.currentTopIndex = breadcrumb.topIndex;
        this.currentSubIndex = breadcrumb.subIndex;
        this.activeTopIndex = breadcrumb.topIndex;
        this.activeSubIndex = breadcrumb.subIndex;
        
        // 更新组件键以触发重新渲染
        this.updateComponentKey();
      }
    }
  },
  created() {
    // 初始化时显示首页
    this.activeTopIndex = -1;
    this.activeSubIndex = -1;
    this.updateComponentKey();
  }
};
</script>

<style>
/* 全局样式 */
page {
  height: 100%;
  margin: 0;
  font-family: Arial, sans-serif;
}

.container {
  display: flex;
  height: 100%;
  position: relative;
}

/* 左侧控制按钮 */
.sidebar-toggle {
  padding: 0 16px;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #666;
  font-size: 15px;
  transition: all 0.3s;
  margin-right: 10px;
}

.sidebar-toggle:hover {
  color: #1890ff;
}

/* 左侧二级导航栏 */
.left-sidebar {
  width: 200px;
  background-color: rgb(0, 21, 41);
  border-right: 1px solid #e0e0e0;
  overflow-y: auto;
  transition: all 0.3s;
}

.left-sidebar.collapsed {
  width: 130px;
  overflow: hidden;
}

.zhuzi {
  color: white;
  height: 40px;
  font-size: 18px;
  text-align: center;
  line-height: 40px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sub-menu-item {
  padding: 14px 16px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  color: rgb(166, 173, 180);
  font-size: 14px;
  white-space: nowrap;
}

.sub-menu-item:hover,
.sub-active {
  color: white;
}

.sub-menu-item text {
  margin-left: 8px;
}

/* 面包屑容器 */
.breadcrumb-container {
  display: flex;
  flex-wrap: wrap;
  padding: 8px 16px;
  background-color: #fff;
  border-bottom: 1px solid #e0e0e0;
  gap: 8px;
}

/* 单个面包屑项 */
.breadcrumb-item {
  padding: 6px 12px;
  background-color: #f0f0f0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.breadcrumb-item:hover {
  background-color: #e0e0e0;
}

.breadcrumb-item.active {
  background-color: #1890ff;
  color: white;
}

.breadcrumb-item.active .close-breadcrumb {
  color: white;
}

/* 首页面包屑特殊样式 - 不能关闭 */
.home-crumb {
  cursor: pointer;
  background-color: #e6f7ff;
}

.home-crumb.active {
  background-color: #1890ff;
  color: white;
}

.close-breadcrumb {
  cursor: pointer;
  font-size: 16px;
  color: #999;
  margin-left: 8px;
  padding: 0 2px;
}

.close-breadcrumb:hover {
  color: #ff4d4f;
}

/* 右侧主区域 */
.right-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: all 0.3s;
}

.right-content.expanded {
  margin-left: -136px;
}

/* 右上方一级导航栏 */
.top-nav {
  display: flex;
  background-color: rgb(240, 240, 240);
  border-bottom: 1px solid #e0e0e0;
  height: 50px;
}

.nav-item {
  padding: 0 24px;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #666;
  font-size: 15px;
  transition: all 0.3s;
}

.nav-item:hover,
.nav-active {
  color: #1890ff;
  font-weight: bold;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  padding: 16px;
  background-color: #f8f9fa;
  overflow-y: auto;
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e0e0e0;
}

.title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.action-buttons {
  display: flex;
  align-items: center;
}

.export-btn {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  background-color: #1890ff;
  color: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.export-btn text {
  margin-left: 5px;
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 12px 16px;
  
  border-radius: 4px;
  
  font-size: 14px;
}

.page-numbers {
  display: flex;
}

.page-numbers text {
  padding: 4px 10px;
  margin: 0 2px;
  cursor: pointer;
  border-radius: 2px;
  font-size: 14px;
}

.current-page {
  background-color: #1890ff;
  color: white;
}
</style>